﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class DanhSachThuoc:IDanhSachThuoc
    {
        int n;
        static List<Thuoc> DSThuoc;
        public DanhSachThuoc()
        {
            DSThuoc = new List<Thuoc>();
        }

        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("DSThuoc.txt", FileMode.Open);
            BinaryReader bReader = new BinaryReader(file);
            while (bReader.PeekChar() != -1)
            {
                int x = bReader.ReadInt32();
                if (x == 1)
                {
                    ThuocTay TT = new ThuocTay();
                    
                    TT.MaThuoc = bReader.ReadString();
                    TT.TenThuoc = bReader.ReadString();
                    TT.GiaBan = decimal.Parse(bReader.ReadString());
                    TT.CongDung = bReader.ReadString();
                    TT.CachDung = bReader.ReadString();
                    TT.ChongChiDinh = bReader.ReadString();
                    TT.HanSuDung = DateTime.Parse(bReader.ReadString());
                    TT.MaNhomThuoc = bReader.ReadString();
                    TT.ThanhPhan = bReader.ReadString();
                    DSThuoc.Add(TT);
                }
                else if (x == 2)
                {
                    ThuocBac TB = new ThuocBac();

                    TB.MaThuoc = bReader.ReadString();
                    TB.TenThuoc = bReader.ReadString();
                    TB.GiaBan = decimal.Parse(bReader.ReadString());
                    TB.CongDung = bReader.ReadString();
                    TB.CachDung = bReader.ReadString();
                    TB.ChongChiDinh = bReader.ReadString();
                    TB.HanSuDung = DateTime.Parse(bReader.ReadString());
                    TB.MaNhomThuoc = bReader.ReadString();
                    TB.TinhThuoc = bReader.ReadString();
                    TB.NguyenLieu = bReader.ReadString();
                    DSThuoc.Add(TB);
                }
            }
            bReader.Close();
            file.Close();
           
        }

        //them thuoc vao danh sach
        public void ThemDSThuoc()
        {
            
            Console.Write("So luong thuoc can them: ");
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.WriteLine("Nhap sai! Vui long nhap lai!");
            }
            for (int i = 0; i < n; i++)
            {
                /*Thuoc Them = new Thuoc();
                Them.NhapThuoc();
                DSThuoc.Add(Them);*/
                int x = -1;
                Console.WriteLine("\nChon: \n\t1: Nhap thuoc Tay \n\t2: Nhap thuoc Bac: ");
                x = int.Parse(Console.ReadLine());
                switch(x)
                {
                    case 1:
                        ThuocTay TT = new ThuocTay();
                        TT.NhapThuoc();
                        TT.ghiFile();
                        DSThuoc.Add(TT);
                        break;
                    case 2:
                        ThuocBac TB = new ThuocBac();
                        TB.NhapThuoc();
                        TB.ghiFile();
                        DSThuoc.Add(TB);
                        break;
                }
            }
        }

        //xuat danh sach thuoc
        public void XuatDSThuoc()
        {
            List<ThuocTay> dsThuocTay = new List<ThuocTay>();
            List<ThuocBac> dsThuocBac = new List<ThuocBac>();
            foreach (Thuoc thuoc in DSThuoc)
            {
                if (thuoc is ThuocTay)
                {
                    ThuocTay thuocTay = (ThuocTay)thuoc;
                    dsThuocTay.Add(thuocTay);
                }
                else if (thuoc is ThuocBac)
                {
                    ThuocBac thuocBac = (ThuocBac)thuoc;
                    dsThuocBac.Add(thuocBac);
                }
            }
            Console.Write("\n---------Danh sach thuoc tay-----------");
            Console.WriteLine("\n---------------------------------------");
            //Console.Write("\nMa   Ten    Gia    Cong dung    Cach dung    HSD    Nhom thuoc    Thanh phan" );
            foreach (ThuocTay thuocTay in dsThuocTay)
            {
                thuocTay.XuatThuoc();
                Console.WriteLine("\n------------------------------");
            }
            Console.WriteLine();
            Console.Write("\n---------Danh sach thuoc bac-----------");
            Console.WriteLine("\n---------------------------------------");
            //Console.Write("\nMa  Ten  Gia  Cong dung  Cach dung  HSD  Nhom thuoc  Tinh thuoc  Nguyen lieu");
            foreach (ThuocBac thuocBac in dsThuocBac)
            {
                thuocBac.XuatThuoc();
                Console.WriteLine("\n------------------------------");
            }
        }

        //tim theo ma thuoc
        public static Thuoc TimTheoMa(string matim)
        {
            foreach (Thuoc tim in DSThuoc)
            {
                if (matim == tim.MaThuoc)
                {
                    return tim;
                }
            }
            return null;
        }

        //tim theo ten thuoc
        public List<Thuoc> TimTheoTen(string tentim)
        {
            List<Thuoc> dstim = new List<Thuoc>();
            foreach (Thuoc tim in DSThuoc)
            {
                if (tim.TenThuoc.Contains(tentim))
                {
                    dstim.Add(tim);
                }
            }
            return dstim;
        }

        //tim theo cong dung
        public List<Thuoc> TimTheoCongDung(string congdung)
        {
            /*foreach (Thuoc tim in DSThuoc)
            {
                if (congdung == tim.CongDung)
                {
                    return tim;
                }
            }
            return null;*/

            List<Thuoc> dstim = new List<Thuoc>();
            foreach (Thuoc tim in DSThuoc)
            {
                if (tim.CongDung.Contains(congdung))
                {
                    dstim.Add(tim);
                }
            }
            return dstim;
        }

        //tim theo nhom thuoc
        public List<Thuoc> TimTheoNhom(string nhomtim)
        {
            List<Thuoc> dstim = new List<Thuoc>();
            foreach (Thuoc tim in DSThuoc)
            {
                if (nhomtim == tim.MaNhomThuoc)
                {
                    dstim.Add(tim);
                }
            }
            return dstim;
        }

        //ham lua chon chuc nang tim kiem
        public void TimKiem()
        {
            int x = -1;
            List<ThuocTay> listThuocTay;
            List<ThuocBac> listThuocBac;
            Console.Write("\nChon chuc nang: \n\t1: Tim theo ma \n\t2: Tim theo ten \n\t3: Tim theo cong dung \n\t4: Tim theo nhom thuoc");
            x = int.Parse(Console.ReadLine());
            switch (x)
            {
                case 1:
                    Console.Write("\nNhap ma can tim: ");
                    string matim = Console.ReadLine();
                    Thuoc kqma = TimTheoMa(matim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (kqma != null)
                    {
                        if (kqma is ThuocTay)
                        {
                            //Console.Write("\nMa \tTen \tGia \tCong dung \tCach dung \tHan su dung \tNhom thuoc \tThanh phan");
                            kqma.XuatThuoc();
                            Console.WriteLine("\n------------------------------");
                        }
                        else if (kqma is ThuocBac)
                        {
                            //Console.Write("\nMa \tTen \tGia \tCong dung \tCach dung \tHan su dung \tNhom thuoc \tTinh thuoc \tNguyen lieu");
                            kqma.XuatThuoc();
                            Console.WriteLine("\n------------------------------");
                        }
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;
                case 2:
                    listThuocTay = new List<ThuocTay>();
                    listThuocBac = new List<ThuocBac>();
                    Console.Write("\nNhap ten can tim: ");
                    string tentim = Console.ReadLine();
                    List<Thuoc> kqten = TimTheoTen(tentim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (kqten.Count > 0)
                    {
                        //Console.Write("\n\tMa \tTen \tGia \tCong dung \tCach dung \tHan su dung \tNhom thuoc");
                        foreach (Thuoc dttim in kqten)
                        {
                            if (dttim is ThuocTay)
                                listThuocTay.Add((ThuocTay)dttim);
                            else if (dttim is ThuocBac)
                                listThuocBac.Add((ThuocBac)dttim);
                        }
                        xuatTim(listThuocTay, listThuocBac);
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;
                case 3:
                    listThuocTay = new List<ThuocTay>();
                    listThuocBac = new List<ThuocBac>();
                    Console.Write("\nNhap cong dung can tim: ");
                    string cdtim = Console.ReadLine();
                    List<Thuoc> kqcd = TimTheoCongDung(cdtim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (kqcd.Count > 0)
                    {
                        //Console.Write("\n\tMa \tTen \tGia \tCong dung \tCach dung \tHan su dung \tNhom thuoc");
                        foreach (Thuoc dttim in kqcd)
                        {
                            if (dttim is ThuocTay)
                                listThuocTay.Add((ThuocTay)dttim);
                            else if (dttim is ThuocBac)
                                listThuocBac.Add((ThuocBac)dttim);
                        }
                        xuatTim(listThuocTay, listThuocBac);
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;
                case 4:
                    listThuocTay = new List<ThuocTay>();
                    listThuocBac = new List<ThuocBac>();
                    Console.Write("\nNhap ma nhom thuoc can tim: ");
                    string maNTTim = Console.ReadLine();
                    List<Thuoc> kqNT = TimTheoNhom(maNTTim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (kqNT.Count > 0)
                    {
                        //Console.Write("\n\tMa \tTen \tGia \tCong dung \tCach dung \tHan su dung \tNhom thuoc");
                        foreach (Thuoc dttim in kqNT)
                        {
                            if (dttim is ThuocTay)
                                listThuocTay.Add((ThuocTay)dttim);
                            else if (dttim is ThuocBac)
                                listThuocBac.Add((ThuocBac)dttim);
                        }
                        xuatTim(listThuocTay, listThuocBac);
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;
            }
        }

        //xoa thuoc trong danh sach
        public bool XoaDSThuoc(string maxoa)
        {
            Thuoc VtXoa = TimTheoMa(maxoa);
            if (VtXoa != null)
            {
                DSThuoc.Remove(VtXoa);
                n--;
                return true;
            }
            return false;
        }

        //sua thong tin thuoc
        public bool SuaDSThuoc(string masua)
        {
            /*Thuoc VtSua = TimTheoMa(masua);
            if (VtSua != null)
            {
                VtSua.TenThuoc = tensua;
                return true;
            }
            return false;*/
            Thuoc thuoctim = TimTheoMa(masua);
            if (thuoctim is ThuocBac)
            {
                ThuocBac TB = (ThuocBac)thuoctim;
                int x = -1;
                while(x != 0)
                {
                    Console.Write("\nChon chuc nang: \n\t1: Ten \n\t2: Gia \n\t3: Cong dung \n\t4: Chong chi dinh \n\t5: Han su dung \n\t6: Nhom \n\t7: Tinh thuoc \n\t8: Nguyen lieu\n\t0: Quay lai\n");
                    while (int.TryParse(Console.ReadLine(), out x) == false)
                    {
                        Console.WriteLine("Chuc nang khong ton tai. Vui long nhap lai!");
                    }
                    switch (x)
                    {
                        case 1:
                            Console.Write("\nNhap ten can sua: ");
                            TB.TenThuoc = Console.ReadLine();
                            break;
                        case 2:
                            Console.Write("Nhap gia can sua: ");
                            TB.GiaBan = decimal.Parse(Console.ReadLine());
                            break;
                        case 3:
                            Console.Write("Nhap cong dung can sua: ");
                            TB.CongDung = Console.ReadLine();
                            break;
                        case 4:
                            Console.Write("Nhap chong chi dinh thuoc: ");
                            TB.ChongChiDinh = Console.ReadLine();
                            break;
                        case 5:
                            Console.Write("Nhap han su dung: ");
                            TB.HanSuDung = DateTime.Parse(Console.ReadLine());
                            break;
                        case 6:
                            Console.Write("Nhap nhom thuoc can sua: ");
                            TB.MaNhomThuoc = Console.ReadLine();
                            break;
                        case 7:
                            Console.Write("Nhap tinh thuoc: ");
                            TB.TinhThuoc = Console.ReadLine();
                            break;
                        case 8:
                            Console.Write("Nhap nguyen lieu: ");
                            TB.NguyenLieu = Console.ReadLine();
                            break;
                        }
                }
                return true;
            }
            else if (thuoctim is ThuocTay)
            {
                ThuocTay TT = (ThuocTay)thuoctim;
                int x = -1;
                while(x != 0)
                {
                    Console.Write("\nChon chuc nang: \n\t1: Ten \n\t2: Gia \n\t3: Cong dung \n\t4: Chong chi dinh \n\t5: Han su dung \n\t6: Nhom \n\t7: Thanh phan\n\t0: Quay lai\n");
                x=int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        Console.Write("\nNhap ten can sua: ");
                        TT.TenThuoc = Console.ReadLine();
                        break;
                    case 2:
                        Console.Write("Nhap gia can sua: ");
                        TT.GiaBan = decimal.Parse(Console.ReadLine());
                        break;
                    case 3:
                        Console.Write("Nhap cong dung can sua: ");
                        TT.CongDung = Console.ReadLine();
                        break;
                    case 4:
                        Console.Write("Nhap chong chi dinh thuoc: ");
                        TT.ChongChiDinh = Console.ReadLine();
                        break;
                    case 5:
                        Console.Write("Nhap han su dung: ");
                        TT.HanSuDung = DateTime.Parse(Console.ReadLine());
                        break;
                    case 6:
                        Console.Write("Nhap nhom thuoc can sua: ");
                        TT.MaNhomThuoc = Console.ReadLine();
                        break;
                    case 7:
                        Console.Write("Nhap thanh phan can sua: ");
                        TT.ThanhPhan = Console.ReadLine();
                        break;
                    }
                }
                return true;
            }
            return false;
        }

        private void xuatTim(List<ThuocTay> listThuocTay, List<ThuocBac> listThuocBac)
        {
            if (listThuocTay.Count > 0)
            {
                Console.Write("\n---------Danh sach thuoc tay-----------");
                Console.WriteLine("\n-----------------------------------------");
                //Console.Write("\nMa   Ten    Gia    Cong dung    Cach dung    HSD    Nhom thuoc   Thanh phan");
                foreach (ThuocTay thuocTay in listThuocTay)
                {
                    thuocTay.XuatThuoc();
                    Console.WriteLine("\n------------------------------");
                }
            }
            if (listThuocBac.Count > 0)
            {
                Console.WriteLine();
                Console.Write("\n---------Danh sach thuoc bac-----------");
                Console.WriteLine("\n--------------------------------------------");
                //Console.Write("\nMa   Ten   Gia   Cong dung   Cach dung    HSD    Nhom thuoc    Tinh thuoc   Nguyen lieu");
                foreach (ThuocBac thuocBac in listThuocBac)
                {
                    thuocBac.XuatThuoc();
                    Console.WriteLine("\n------------------------------");
                }
            }
        }
    }
}
